package com.atguigu.yygh.hander;

import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @author A
 * @version 1.0
 * @description: 全局异常处理器
 * @date 2025/3/10 20:46
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value ={Exception.class} )
    public R allExceptionHandler(Exception e){
        e.printStackTrace();
        return R.error().message("服务器内部错误");
    }
    @ExceptionHandler(value = {ArithmeticException.class})
    public R arithExceptionHandler(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("算术异常");
    }
    @ExceptionHandler(value = {SQLException.class})
    public R SQLExceptionHandler(SQLException e){
        e.printStackTrace();
        return R.error().message("SQL异常");
    }
    //自定义异常
    @ExceptionHandler(value = {YyghException.class})
    public R YyghExceptionHandler(YyghException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return R.error().message(e.getMessage()).code(e.getCode());
    }
}
